<?php
/**
 * 教室回访类
 */
namespace app\admin\controller;

use think\App;
use think\facade\View;
use app\admin\model\Classtype;
use app\admin\model\Classgrade;
use app\admin\model\Usermodel;
use app\admin\model\Classroom as croom;
use app\admin\validate\Classroom as validate;

class Crvisit extends Common
{
    protected $typemodel;
    protected $grademodel;
    protected $usermodel;

    public function __construct(App $app)
    {
        $this->validate = validate::class;
        $this->localModel = new croom();
        parent::__construct($app);
        $this->usermodel = new Usermodel();
        $this->typemodel = new Classtype();
        $this->grademodel = new Classgrade();
    }

    public function index()
    {
        //获取教室等级
        $typeData = $this->typemodel->findAllInfo('id, name', [['status', '=', 1]]);
        //获取教室类型
        $gradeData = $this->grademodel->findAllInfo('id, name', [['status', '=', 1]]);

        $where = $showDataArr = [];

        $dataArr = $this->request->request('search/a', []);//print_r($dataArr);

        if($dataArr['name'])  $where[]       = ['c.name', 'like', "%".$dataArr['name']."%"];
        if($dataArr['sheng'])  $where[]      = ['adress', 'like', "%".$dataArr['sheng'].",".$dataArr['shi']."%"];
        if($dataArr['codekey'])  $where[]    = ['codekey', 'like', "%".$dataArr['codekey']."%"];
        if($dataArr['status'])  $where[]     = ['c.status', '=', $dataArr['status']];
        if($dataArr['type'])  $where[]       = ['type', '=', $dataArr['type']];
        if($dataArr['level'])  $where[]      = ['level', '=', $dataArr['level']];
        if($dataArr['start_time'])  $where[] = ['start_time', '<=', $dataArr['start_time']];
        if($dataArr['end_time'])  $where[]   = ['end_time', '>=', $dataArr['end_time']];
        if(session('adminUserInfo.rid') == 7){
            //代理管理员只能看到自己管理下的教室信息
            $where[] = ['agent_uid', '=', session('adminUserInfo.uid')];
        }
        if(session('adminUserInfo.cid'))  $where[] = ['c.id', '=', session('adminUserInfo.cid')];

        $list = $this->localModel->findAll($where, $this->request->param());
        $showDataArr = ['classRoomData'=>$list['data'], 'pageData'=>$list['page'], 'page'=>$this->request->get('page'), 'typeData'=>$typeData, 'gradeData'=>$gradeData];
        View::assign(array_merge($dataArr, $showDataArr));
        return view();
    }

    public function add(){
        //获取教室等级
        $typeData = $this->typemodel->findAllInfo('id, name', [['status', '=', 1]]);

        //获取教室类型
        $gradeData = $this->grademodel->findAllInfo('id, name', [['status', '=', 1]]);

        //获取代理员
        $userData = $this->usermodel->findAllUserInfo([['u.rid', '=', 7], ['u.status', '=', 1]]);

        $id = $this->request->get('id', 0);

        $showData = ['typeData'=>$typeData, 'gradeData'=>$gradeData, 'userData'=>$userData];
        if($id){
            $showData['id'] = $id;
            $showData['detail'] = $this->localModel->findDetaiInfoById('*', $id);
            $showData['detail']['adress'] = explode(',', $showData['detail']['adress']);
            View::assign($showData);
            return view('edit');
        }else{
            View::assign($showData);
            return view();
        }
    }

    public function save(){
        $dataArr = $this->request->request('info/a', []);
        $id = $this->request->post('id', 0);

        if($msg = $this->checkData($dataArr, 'classroom'))  return json(['result'=>'fail', 'title'=>$msg]);
        $dataArr['adress'] = implode(',', $dataArr['adress']);

        $res = $this->localModel->saveInfo($dataArr, $id);

        if(!$res)  return json(['result'=>'fail', 'title'=>'操作失败！']);

        $this->logsData = ['cid'=>$id ? $id : $res, 'action'=>$id ? 'edit' : 'add', 'msg'=>$dataArr['name']];
        return json(['result'=>'success', 'title'=>'恭喜您操作成功！', 'url'=>(string)url('Classroom/index')]);
    }


}
